<template>
    <div>
        <el-table max-height="510px" default-expand-all highlight-current-row row-key="id"
            :tree-props="{ children: 'children' }" :data="menuList" border style="width: 100%">
            <el-table-column prop="id" label="菜单编号" width="180" />
            <el-table-column prop="title" label="菜单名称" width="180" />
            <el-table-column prop="pid" label="上级菜单" />
            <el-table-column prop="icon" label="菜单图标">
                <template v-slot="item">
                    <div>
                        <el-icon  type="success">
                            <component :is="item.row.icon"></component>
                        </el-icon>
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="url" label="菜单地址" />
            <el-table-column prop="status" label="状态">
                <template v-slot="item">
                    <div>
                        <el-tag v-if="item.row.status == 1" type="success">启用</el-tag>
                        <el-tag v-else type="danger">禁用</el-tag>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template v-slot="item">
                    <div>
                        <el-button type="primary" size="small" @click="edit(item.row.id)">编辑</el-button>
                        <el-button type="danger" size="small" @click="del(item.row.id)">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script lang="ts" setup>
import { defineEmits,computed } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus'
// 仓库
import { useStore } from 'vuex'
// 引入接口
import { menuDel } from '@/request/app'
let emit = defineEmits(['edit'])
// 编辑按钮
let edit = (id:Number) => {
    emit('edit',id)
}
// 仓库
let store = useStore()
const menuList=computed(()=>store.getters['menu/getMenuList'])
const getMenu = ()=>{
    store.dispatch('menu/getMenuListAction')
}
getMenu()
// 删除事件
let del = (id: any) => {
    ElMessageBox.confirm(
        '确认删除吗?',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            // 点击确认  调用删除接口
            menuDel({ id })
                .then(res => {
                    console.log(res, '删除成功');
                    if (res.code == 200) {
                        // 提示信息
                        ElMessage.success(res.msg)
                        // 重新加载列表
                        getMenu()
                    } else {
                        // 提示信息
                        ElMessage.success(res.msg)
                    }
                })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '删除失败',
            })
        })
}
</script>

<style scoped>
</style>
